package org.sample.sss.controller;

import org.apache.commons.lang3.StringUtils;
import org.sample.sss.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.http.HttpResponse;

@Controller
public class LoginController {
    @RequestMapping("/login")
    @ResponseBody
    public String login(HttpServletRequest request, HttpServletResponse response, User user){

        if(user != null && StringUtils.isNoneEmpty(user.getUserName(), user.getPassword()) &&
                "admin".equals(user.getUserName()) && "admin".equals(user.getPassword())){
            //校验成功
            request.getSession().setAttribute("loginUsername", user.getUserName());
            String accessUrl = (String)request.getSession().getAttribute("accessUrl");
            System.out.println("login sucess! accessUrl=" + accessUrl);
            return accessUrl;
        }else{
            //校验失败
            request.setAttribute("errorMsg", "用户名密码错误，请重新登录！");
            System.out.println("登录失败。跳转登录页");
            return "fail";
        }
    }
}
